﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Terraria.Localization;

namespace Terraria.ModLoader
{
	// Token: 0x0200018E RID: 398
	public static class ErrorLogger
	{
		// Token: 0x06000FA8 RID: 4008 RVA: 0x00404358 File Offset: 0x00402558
		internal static void LogBuildError(string errorText)
		{
			Directory.CreateDirectory(ErrorLogger.LogPath);
			File.WriteAllText(ErrorLogger.CompileErrorPath, errorText);
			Console.WriteLine(errorText);
			Interface.errorMessage.SetMessage(errorText);
			Interface.errorMessage.SetGotoMenu(10001);
			Interface.errorMessage.SetFile(ErrorLogger.CompileErrorPath);
		}

		// Token: 0x06000FA9 RID: 4009 RVA: 0x004043AC File Offset: 0x004025AC
		internal static void LogCompileErrors(CompilerErrorCollection errors, bool forWindows)
		{
			string text = Language.GetTextValue("tModLoader.BuildErrorCompilingError") + Environment.NewLine + Environment.NewLine;
			string text2 = "";
			if (!forWindows && ModLoader.windows)
			{
				text2 = Language.GetTextValue("tModLoader.BuildErrorModCompileFolderHint") + Environment.NewLine + Environment.NewLine;
			}
			Console.WriteLine(text + text2);
			Directory.CreateDirectory(ErrorLogger.LogPath);
			CompilerError compilerError = null;
			using (StreamWriter streamWriter = File.CreateText(ErrorLogger.CompileErrorPath))
			{
				foreach (object obj in errors)
				{
					CompilerError compilerError2 = (CompilerError)obj;
					streamWriter.WriteLine(compilerError2 + Environment.NewLine);
					Console.WriteLine(compilerError2);
					if (!compilerError2.IsWarning && compilerError == null)
					{
						compilerError = compilerError2;
					}
				}
			}
			Interface.errorMessage.SetMessage(text + text2 + compilerError);
			Interface.errorMessage.SetGotoMenu(10001);
			Interface.errorMessage.SetFile(ErrorLogger.CompileErrorPath);
		}

		// Token: 0x06000FAA RID: 4010 RVA: 0x004044D8 File Offset: 0x004026D8
		internal static void LogDllBuildError(string modDir)
		{
			Directory.CreateDirectory(ErrorLogger.LogPath);
			string text = Language.GetTextValue("tModLoader.BuildErrorMissingDllFilesFor", Path.GetFileName(modDir)) + Environment.NewLine + Environment.NewLine + string.Join(Environment.NewLine, ErrorLogger.buildDllLines);
			File.WriteAllText(ErrorLogger.CompileErrorPath, text);
			Console.WriteLine(text);
			Interface.errorMessage.SetMessage(text);
			Interface.errorMessage.SetGotoMenu(10001);
			Interface.errorMessage.SetFile(ErrorLogger.CompileErrorPath);
		}

		// Token: 0x06000FAB RID: 4011 RVA: 0x0040455C File Offset: 0x0040275C
		internal static void LogDependencyError(string error)
		{
			Directory.CreateDirectory(ErrorLogger.LogPath);
			string text = Path.Combine(ErrorLogger.LogPath, "Loading Errors.txt");
			File.WriteAllText(text, error);
			Console.WriteLine(error);
			Interface.errorMessage.SetMessage(error);
			Interface.errorMessage.SetGotoMenu(10006);
			Interface.errorMessage.SetFile(text);
		}

		// Token: 0x06000FAC RID: 4012 RVA: 0x004045B8 File Offset: 0x004027B8
		internal static void LogLoadingError(string modFile, Version modBuildVersion, Exception e, bool recipes = false)
		{
			Directory.CreateDirectory(ErrorLogger.LogPath);
			string text = ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Loading Errors.txt";
			using (StreamWriter streamWriter = File.CreateText(text))
			{
				streamWriter.WriteLine(e.Message);
				streamWriter.WriteLine(e.StackTrace);
				for (Exception innerException = e.InnerException; innerException != null; innerException = innerException.InnerException)
				{
					streamWriter.WriteLine();
					streamWriter.WriteLine("Inner Exception:");
					streamWriter.WriteLine(innerException.Message);
					streamWriter.WriteLine(innerException.StackTrace);
				}
			}
			string text2;
			if (recipes)
			{
				text2 = Language.GetTextValue("tModLoader.LoadErrorRecipes", modFile);
			}
			else
			{
				text2 = Language.GetTextValue("tModLoader.LoadError", modFile);
			}
			if (modBuildVersion != ModLoader.version)
			{
				text2 = text2 + "\n" + Language.GetTextValue("tModLoader.LoadErrorVersionMessage", modBuildVersion, ModLoader.versionedName);
			}
			text2 = text2 + "\n" + Language.GetTextValue("tModLoader.LoadErrorDisabledSeeBelowForError");
			text2 = string.Concat(new string[]
			{
				text2,
				"\n\n",
				e.Message,
				"\n",
				e.StackTrace
			});
			if (Main.dedServ)
			{
				Console.WriteLine(text2);
			}
			Interface.errorMessage.SetMessage(text2);
			Interface.errorMessage.SetGotoMenu(10006);
			Interface.errorMessage.SetFile(text);
			if (!string.IsNullOrEmpty(e.HelpLink))
			{
				Interface.errorMessage.SetWebHelpURL(e.HelpLink);
			}
		}

		// Token: 0x06000FAD RID: 4013 RVA: 0x00404748 File Offset: 0x00402948
		internal static void LogException(Exception e, string msg = "The game has crashed!")
		{
			object obj = ErrorLogger.logExceptionLock;
			lock (obj)
			{
				Directory.CreateDirectory(ErrorLogger.LogPath);
				string text = ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Runtime Error.txt";
				using (StreamWriter streamWriter = File.CreateText(text))
				{
					streamWriter.WriteLine(e.Message);
					streamWriter.WriteLine(e.StackTrace);
					for (Exception innerException = e.InnerException; innerException != null; innerException = innerException.InnerException)
					{
						streamWriter.WriteLine();
						streamWriter.WriteLine("Inner Exception:");
						streamWriter.WriteLine(innerException.Message);
						streamWriter.WriteLine(innerException.StackTrace);
					}
				}
				Interface.errorMessage.SetMessage(string.Concat(new string[]
				{
					msg,
					"\n\n",
					e.Message,
					"\n",
					e.StackTrace
				}));
				Interface.errorMessage.SetGotoMenu(0);
				Interface.errorMessage.SetFile(text);
				Main.gameMenu = true;
				Main.menuMode = 10005;
			}
		}

		// Token: 0x06000FAE RID: 4014 RVA: 0x0000E9FF File Offset: 0x0000CBFF
		internal static void LogModBrowserException(Exception e)
		{
			ErrorLogger.LogException(e, "The game has crashed accessing Web Resources!");
		}

		// Token: 0x06000FAF RID: 4015 RVA: 0x004048A4 File Offset: 0x00402AA4
		internal static void LogModPublish(string message)
		{
			string text = ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Network Error.txt";
			using (StreamWriter streamWriter = File.CreateText(text))
			{
				streamWriter.WriteLine(message);
			}
			Interface.errorMessage.SetMessage("The Mod Browser server response:\n\n" + message);
			Interface.errorMessage.SetGotoMenu(10001);
			Interface.errorMessage.SetFile(text);
			Main.gameMenu = true;
			Main.menuMode = 10005;
		}

		// Token: 0x06000FB0 RID: 4016 RVA: 0x00404938 File Offset: 0x00402B38
		internal static void LogModUnPublish(string message)
		{
			string text = ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Network Error.txt";
			using (StreamWriter streamWriter = File.CreateText(text))
			{
				streamWriter.WriteLine(message);
			}
			Interface.errorMessage.SetMessage("The Mod Browser server response:\n\n" + message);
			Interface.errorMessage.SetGotoMenu(10011);
			Interface.errorMessage.SetFile(text);
			Main.gameMenu = true;
			Main.menuMode = 10005;
		}

		// Token: 0x06000FB1 RID: 4017 RVA: 0x004049CC File Offset: 0x00402BCC
		internal static void LogMissingMods(string msg)
		{
			Directory.CreateDirectory(ErrorLogger.LogPath);
			string text = Path.Combine(ErrorLogger.LogPath, "Missing Mods.txt");
			File.WriteAllText(text, msg);
			Console.WriteLine(msg);
			Interface.errorMessage.SetMessage(msg);
			Interface.errorMessage.SetGotoMenu(0);
			Interface.errorMessage.SetFile(text);
			Main.gameMenu = true;
			Main.menuMode = 10005;
		}

		// Token: 0x06000FB2 RID: 4018 RVA: 0x00404A34 File Offset: 0x00402C34
		public static void Log(string message)
		{
			object obj = ErrorLogger.logLock;
			lock (obj)
			{
				Directory.CreateDirectory(ErrorLogger.LogPath);
				using (StreamWriter streamWriter = File.AppendText(ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Logs.txt"))
				{
					streamWriter.WriteLine(message);
				}
			}
		}

		// Token: 0x06000FB3 RID: 4019 RVA: 0x00404AB8 File Offset: 0x00402CB8
		public static void Log(object param, bool alternateOutput = false)
		{
			object obj = ErrorLogger.logLock;
			lock (obj)
			{
				Directory.CreateDirectory(ErrorLogger.LogPath);
				using (StreamWriter streamWriter = File.AppendText(ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Logs.txt"))
				{
					if (!alternateOutput)
					{
						streamWriter.WriteLine(param.ToString());
					}
					else
					{
						streamWriter.WriteLine("Object type: " + param.GetType());
						foreach (PropertyInfo propertyInfo in param.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
						{
							streamWriter.Write(string.Concat(new object[]
							{
								"PROPERTY ",
								propertyInfo.Name,
								" = ",
								propertyInfo.GetValue(param, null),
								"\n"
							}));
						}
						foreach (FieldInfo fieldInfo in param.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
						{
							streamWriter.Write(string.Concat(new object[]
							{
								"FIELD ",
								fieldInfo.Name,
								" = ",
								(fieldInfo.GetValue(param).ToString() != "") ? fieldInfo.GetValue(param) : "(Field value not found)",
								"\n"
							}));
						}
						foreach (MethodInfo methodInfo in param.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
						{
							streamWriter.Write("METHOD " + methodInfo.Name + "\n");
						}
						int num = 0;
						foreach (ConstructorInfo constructorInfo in param.GetType().GetConstructors(BindingFlags.Public | BindingFlags.NonPublic))
						{
							num++;
							streamWriter.Write(string.Concat(new object[]
							{
								"CONSTRUCTOR ",
								num,
								" : ",
								constructorInfo.Name,
								"\n"
							}));
						}
					}
				}
			}
		}

		// Token: 0x06000FB4 RID: 4020 RVA: 0x00404D1C File Offset: 0x00402F1C
		public static void ClearLogs()
		{
			object obj = ErrorLogger.logLock;
			lock (obj)
			{
				Directory.CreateDirectory(ErrorLogger.LogPath);
				string[] array = new string[]
				{
					ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Logs.txt",
					ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Network Error.txt",
					ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Runtime Error.txt",
					ErrorLogger.LogPath + Path.DirectorySeparatorChar.ToString() + "Loading Errors.txt",
					ErrorLogger.CompileErrorPath
				};
				foreach (string path in array)
				{
					try
					{
						File.Delete(path);
					}
					catch (Exception)
					{
					}
				}
			}
		}

		// Token: 0x06000FB5 RID: 4021 RVA: 0x00404E20 File Offset: 0x00403020
		public static void LogMulti(IEnumerable<Action> logCalls)
		{
			if (Main.dedServ)
			{
				foreach (Action action in logCalls)
				{
					action();
				}
				return;
			}
			Action[] list = logCalls.ToArray<Action>();
			list[0]();
			if (list.Length > 1)
			{
				Interface.errorMessage.OverrideContinueAction(delegate
				{
					ErrorLogger.LogMulti(list.Skip(1));
				});
			}
		}

		// Token: 0x04001344 RID: 4932
		public static readonly string LogPath = Path.Combine(Main.SavePath, "Logs");

		// Token: 0x04001345 RID: 4933
		internal static string CompileErrorPath = Path.Combine(ErrorLogger.LogPath, "Compile Errors.txt");

		// Token: 0x04001346 RID: 4934
		private static readonly string[] buildDllLines = new string[]
		{
			"Must have either All.dll or both of Windows.dll and Mono.dll",
			"All.dll must not have any references to Microsoft.Xna.Framework or FNA",
			"Windows.dll must reference the windows Terraria.exe and Microsoft.Xna.Framework.dll",
			"Mono.dll must reference a non-windows Terraria.exe and FNA.dll"
		};

		// Token: 0x04001347 RID: 4935
		private static object logExceptionLock = new object();

		// Token: 0x04001348 RID: 4936
		private static object logLock = new object();
	}
}
